local Widget = require("widgets/widget")
local Text = require("widgets/text")
local ImageButton = require("widgets/imagebutton")
require("fonts")
local t_mana = require "libs/timermanager"
local w_util = require "libs/worldutil"
local p_util = require "libs/playerutil"
local t_util = require "libs/tableutil"

local huxiTimer = Class(Widget, function(self)
    Widget._ctor(self, "huxiTimer")
    self:SetVAnchor(ANCHOR_TOP)
    self:SetHAnchor(ANCHOR_LEFT)
    local screen_w, screen_h = TheSim:GetScreenSize()
    local offx, offy = 120, 50             -- 起始偏移量, 以我的电脑为准，等比放缩
    self.num_col = 10                      -- 一行显示几个图标
    self.timer_btn_size = 50 * 1920 / screen_w
    self:SetPosition(1920/screen_w*offx,1080/screen_h*(-offy))
    w_util:SetBindEvent("Mod_ShroomCake_Timer", t_mana.Process)
    w_util:SetBindEvent("entercharacterselect", t_mana.ResetPlayer)
    p_util:SetBindEvent("isghostmodedirty", function(pc)
        if p_util:IsDead() then t_mana:ResetPlayer() end
    end)
    self.pertask = self.inst:DoPeriodicTask(1, function()
        self:OnUpdate()
    end)
end)

function huxiTimer:ForceUpdate()
    if self.timerwidgets then self.timerwidgets:Kill() end
    self.timerwidgets = self:AddChild(self:BuildWidgets())
end

function huxiTimer:OnUpdate()
    if not self.timerwidgets or t_mana:GetUIRefresh() then
        t_mana:SetUIRefresh(false)
        self:ForceUpdate()
    else
        local timers = t_mana:GetTimers()
        local btns = self.timerwidgets.btns
        for id,btn in pairs(btns)do
            local data = timers[id]
            if data then
                btn.thetext:SetColour(unpack(data.color))
                btn.thetext:SetString(data.text)
            else
                self:ForceUpdate()
            end
        end
    end
end


function huxiTimer:BuildWidgets()    
    local spacing_y = self.timer_btn_size * 1.7
    local spacing_x = self.timer_btn_size * 1.5
    local text_posy = self.timer_btn_size * -1
    local text_size = self.timer_btn_size * 0.6
    local w = Widget("allwidgets")
    w.btns = {}
    local all_timers = t_mana:GetTimers()
    local i = 1
    for tp, timers in pairs(all_timers)do
        for id, timer in pairs(timers)do
            local btn = w:AddChild(Widget("btn_text"))
            local x_pos = (i-1)%self.num_col * spacing_x
            local y_pos = math.floor((i-1)/self.num_col) * spacing_y
            btn:SetPosition(x_pos, -y_pos)
            btn.imgbtn = btn:AddChild(ImageButton(timer.atlas, timer.image))

            local sizeX, sizeY = btn.imgbtn:GetSize()
            local trans_scale = math.min(self.timer_btn_size / sizeX, self.timer_btn_size / sizeY)
            btn.imgbtn:SetNormalScale(trans_scale)
            btn.imgbtn:SetFocusScale(trans_scale * 1.2)

            btn.thetext = btn:AddChild(Text(NUMBERFONT, text_size, timer.text, timer.color))
            btn.thetext:SetPosition(0, text_posy)

            btn:SetTooltip(timer.description)
            btn:SetTooltipPos(0, text_posy*1.3 ,0)
            w.btns[id] = btn
            i = i+1
        end
    end
    return w
end

return huxiTimer